import React, { PureComponent } from 'react'
import { LoginWrapper, LoginBox, Input, Button } from './style'
import { connect } from 'react-redux'
import { actionCreators } from './store'
import { Redirect } from 'react-router-dom'

class Login extends PureComponent {
  render() {
    const { isLogin } = this.props
    if (!isLogin) {
      return (
        <LoginWrapper>
          <LoginBox>
            <Input placeholder='账号' innerRef={(input) => { this.user = input }} />
            <Input placeholder='密码' type='password' innerRef={(input) => { this.pwd = input }} />
            <Button onClick={() => this.props.login(this.user, this.pwd)}>登录</Button>
          </LoginBox>
        </LoginWrapper>
      )
    } else {
      return <Redirect to='/' />
    }
  }

}

const mapState = (state) => ({
  isLogin: state.getIn(['login', 'login'])
})

const mapDispatch = (dispatch) => ({
  login(user, pwd) {
    dispatch(actionCreators.login(user, pwd))
  }
})

export default connect(mapState, mapDispatch)(Login)